package com.ldc.commonres.common

import android.util.SparseArray
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.IdRes
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.recyclerview.widget.RecyclerView
import com.ldc.commonlibs.conf.AppConf
import com.ldc.commonlibs.listener.OnRowListener
import com.ldc.commonlibs.listener.OnRowListener2
import com.ldc.commonres.R
import com.ldc.commonres.uts.GlideUts
import java.io.File

class BaseRvViewHolder constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {
    // 封装
    private val viewMap: SparseArray<View> = SparseArray(16)

    companion object {
        @JvmStatic
        fun createViewHolder(view: View): BaseRvViewHolder {
            return BaseRvViewHolder(view)
        }

        @JvmStatic
        fun createViewHolder(parent: ViewGroup, @LayoutRes layoutId: Int): BaseRvViewHolder {
            val view: View = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
            return BaseRvViewHolder(view)
        }

        @JvmStatic
        fun createViewHolder(
            parent: ViewGroup,
            viewType: Int,
            @LayoutRes layoutId: Int,
        ): BaseRvViewHolder {
            return if (viewType == AppConf.EMPTY_NOT_DATA) createViewHolder(
                parent,
                R.layout.res_common_status_view
            ) else createViewHolder(parent, layoutId)
        }

    }

    fun findView(@IdRes id: Int): View? {
        var v: View? = viewMap.get(id)
        if (null == v) {
            v = itemView.findViewById(id)
            viewMap.put(id, v)
        }
        return v
    }


    fun setText(@IdRes id: Int, text: String = "") {
        findView(id)?.let {
            if (it is TextView) {
                it.text = text
            } else if (it is EditText) {
                it.setText(text)
            }
        }
    }

    fun setText(@IdRes id: Int, text: CharSequence = "") {
        findView(id)?.let {
            if (it is TextView) {
                it.text = text
            } else if (it is EditText) {
                it.setText(text)
            }
        }
    }

    fun setText(@IdRes id: Int, @StringRes text: Int) {
        findView(id)?.let {
            if (it is TextView) {
                it.setText(text)
            } else if (it is EditText) {
                it.setText(text)
            }
        }
    }

    fun setImage(@IdRes id: Int, src: String) {
        findView(id)?.let {
            if (it is ImageView) {
                GlideUts.setImage(itemView.context, src, it)
            }
        }
    }

    fun setImage(@IdRes id: Int, src: File) {
        findView(id)?.let {
            if (it is ImageView) {
                GlideUts.setImage(itemView.context, src, it)
            }
        }
    }

    fun setImage(@IdRes id: Int, src: Int) {
        findView(id)?.let {
            if (it is ImageView) {
                GlideUts.setImage(itemView.context, src, it)
            }
        }
    }


    fun <T> addOnRowListener(@IdRes id: Int, data: T, listener: OnRowListener<T>?) {
        findView(id)?.let { it ->
            it.setOnClickListener { view ->
                listener?.onRow(view, data)
            }
        }
    }

    fun <T, E> addOnRowListener2(
        @IdRes id: Int,
        data: T,
        data2: E,
        listener: OnRowListener2<T, E>?
    ) {
        findView(id)?.let { it ->
            it.setOnClickListener { view ->
                listener?.onRow(view, data, data2)
            }
        }
    }

    fun <T> addOnRowListener(view: View, data: T, listener: OnRowListener<T>?) {
        view.let { it ->
            it.setOnClickListener { v ->
                listener?.onRow(v, data)
            }
        }
    }

    fun visibility(@IdRes id: Int, show: Boolean) {
        findView(id)?.let {
            it.visibility = if (show) View.VISIBLE else View.GONE
        }
    }
}